package com.itic.codeGen.codeGenerator.workLine;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import com.itic.codeGen.codeGenerator.util.CommonUtils;
import com.itic.codeGen.codeGenerator.util.ContentUtils;

public class FileGenerator {
    /**
     * 
     * @param target
     */
    public void generateClass(TargetJava target) {
	// get directory path
	File directory = CommonUtils.getDirectory(target.getProject(),
	        target.getPkage());
	// set output File
	String fileName = target.getClzName() + CommonUtils.JAVA_FILE;
	File output = new File(directory, fileName);
	
	//if (!output.exists()) {
	    String content = ContentUtils.getJavaContent(target);
	    // output
	    this.writeFile(output, content);
	//}
    }

    /**
     * 
     * @param target
     */
    public void generateServiceClass(TargetJava target) {
	// get directory path
	File directory = CommonUtils.getDirectory(target.getProject(),
	        target.getPkage()+".impl");
	// set output File
	String fileName = target.getClzName() + "Impl" + CommonUtils.JAVA_FILE ;
	File output = new File(directory, fileName);
	
	//if (!output.exists()) {
	    String content = ContentUtils.getJavaContentServiceImpl(target);
	    // output
	    this.writeFile(output, content);
	//}
    }
    /**
     * 
     * @param target
     */
    public void generateInterface(TargetJava target) {
	// get directory path
	File directory = CommonUtils.getDirectory(target.getProject(),
	        target.getPkage());
	// set output File
	String fileName = target.getClzName() + CommonUtils.JAVA_FILE;
	File output = new File(directory, fileName);
	//if (!output.exists()) {
	    String content = ContentUtils.getInterfaceContent(target);
	    // output
	    this.writeFile(output, content);
	//}
    }

    /**
     * 
     * @param target
     */
    public void generateServiceInterface(TargetJava target) {
	// get directory path
	File directory = CommonUtils.getDirectory(target.getProject(),
	        target.getPkage());
	// set output File
	String fileName = target.getClzName() + CommonUtils.JAVA_FILE;
	File output = new File(directory, fileName);
	//if (!output.exists()) {
	    String content = ContentUtils.getInterfaceContentService(target);
	    // output
	    this.writeFile(output, content);
	//}
    }
    /**
     * 
     * @param target
     */
    public void generateXML(TargetXML target) {
	// get directory path
	File directory = CommonUtils.getDirectory(target.getProject(),
	        target.getPkage());
	// set output File
	String fileName = target.getXmlName() + CommonUtils.XML_FILE;
	File output = new File(directory, fileName);
	//if (!output.exists()) {
	    String content = ContentUtils.getXMLContent(target);
	    // output
	    this.writeFile(output, content);
	//}
    }

    /**
     * Wirte File
     * 
     * @param file
     * @param content
     */
    private void writeFile(File file, String content) {
	BufferedWriter bw = null;
	try {
	    bw = new BufferedWriter(new FileWriter(file, false));
	    bw.write(content);
	} catch (IOException ex) {

	} finally {
	    if (null != bw) {
		try {
		    bw.close();
		} catch (IOException e) {
		}
	    }
	}
    }

}
